package k;

import k.dto.Emp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by kzhou on 2015/4/22.
 */
public class HelperClazz {
    private static Emp EMP ;

    public static Emp getEMP() {
        return EMP;
    }

    public static void setEMP(Emp EMP) {
        HelperClazz.EMP = EMP;
    }

    private final static Logger log = LoggerFactory.getLogger(HelperClazz.class);
    private static final String DEFAULT_TEXT_CONTENT_TYPE = "text/plain; charset=utf8";
    private static final String DEFAULT_JSON_CONTENT_TYPE = "application/json; charset=utf8";
    public static final String LOGIN_SESSION_NAME = "smart_city_flrj_login";
    public static final String CG_URL = "http://192.168.7.6:9001/";//调用下派给城管的接口地址前缀

    //各应用上传路径
//    public static final String UPLOAD_PATH = "/usr/home/smart_city_files/";
    public static final String UPLOAD_PATH = "D://smart_city_files/";
    public static final String MZ_PATH = UPLOAD_PATH + "mz";
    public static final String OA_PATH = UPLOAD_PATH + "oa";
    public static final String YJ_PATH = UPLOAD_PATH + "yj";
    public static final String CMS_PATH = UPLOAD_PATH + "cms";
    public static final String FTP_PATH = "/home/newspaces/smart_city_files/";

    //各应用附件资源访问根目录
    public static final String SHOW_PATH_ROOT = "http://202.97.207.106:9091/mh/";
    public static final String SHOW_PATH_ROOT2 = "http://localhost:8020/mz/";
    public static final String MZ_SHOW_PATH = SHOW_PATH_ROOT + "/mz";
    public static final String OA_SHOW_PATH = SHOW_PATH_ROOT + "/oa";
    public static final String YJ_SHOW_PATH = SHOW_PATH_ROOT + "/yj";
    public static final String CMS_SHOW_PATH = SHOW_PATH_ROOT + "/cms";

    //用户管理接口IP
    public static final String manageIp = "http://202.97.207.85:8094/flej/";
    //OA用户管理接口IP
    public static final String OAManageIP = "http://202.97.207.106:99/logincheck_new.php?";
    //事件推送接口IP
    public static final String eventIp = "http://202.97.207.85:8091/SmartETL";
    //暂时不知道干啥用的调的接口。。。
    public static final String argsIp = "http://202.97.207.85:8091/SmartCity";
    //MP3文件接口IP
    public static final String mp3Ip = "http://192.168.230.100:8099/";
    //lzx接口
    public static final String lzxIp = "http://202.97.207.85:8094";
    //农业接口
    public static final String AgricultureIp = "http://202.97.207.85:8094";


//    //用户管理接口IP
//    public static final String manageIp = "http://10.0.0.5:8081/flej/";
//    //OA用户管理接口IP
//    public static final String OAManageIP = "http://10.0.0.8:99/logincheck_smart.php?";
//    //事件推送接口IP
//    public static final String eventIp = "http://10.0.0.6:8080/SmartETL";
//    //OA返回待办列表
//    public static final String argsIp = "http://10.0.0.8:99/SmartCity";
//    //MP3文件接口IP
//    public static final String mp3Ip = "http://192.168.230.100:8099/";

    //url中文编码
    public static String encode(String s) {
        try {
            return URLEncoder.encode(s, "utf8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    //url中文解码
    public static String decode(String s) {
        try {
            return URLDecoder.decode(s, "utf8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

//    //获得登陆信息id
//    //0emp_id-1emp_name-2org_id-3roleids-4orgName
//    public static String getLoginInfo(HttpServletRequest request, int idx) {
////        Sys_emp user =EMP;
//        String[] loginInfos=new String[5];
//        if(!isEmpty(user)) {
//            loginInfos[0] = user.getEmp_id().toString();
//            loginInfos[1] = user.getEmp_name();
//            loginInfos[2] = user.getOrg_id().toString();
//            loginInfos[3] = user.getRoleIds();
//            loginInfos[4] = user.getOrg_name();
//            return decode(loginInfos[idx]);
//        }else{
//            return null;
//        }
//
//    }

    //response返回内容
    public static void renderText(HttpServletResponse response, String text) {
        PrintWriter writer = null;
        try {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Access-Control-Allow-Origin","*");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType(DEFAULT_TEXT_CONTENT_TYPE);
            writer = response.getWriter();
            writer.write(text);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                writer.close();
        }
    }

    //response返回内容
    public static void renderJson(HttpServletResponse response, String text) {
        PrintWriter writer = null;
        try {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Access-Control-Allow-Origin","*");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType(DEFAULT_JSON_CONTENT_TYPE);
            writer = response.getWriter();
            writer.write(text);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                writer.close();
        }
    }

    //生成随机名称，用于上传文件命名
    public static String getRandomName() {
        Random r = new Random();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
        StringBuffer sb = new StringBuffer();
        sb.append(r.nextInt(100));
        sb.append(r.nextInt(100));
        sb.append("_");
        sb.append(sdf.format(new Date()));
        sb.append("_");
        sb.append(r.nextInt(100));
        sb.append(r.nextInt(100));
        return sb.toString();
    }

    //获得文件名后缀
    public static String getFileExt(String fileName) {
        int idx = fileName.lastIndexOf(".");
        String ext = null;
        if (idx != -1) {
            ext = fileName.substring(idx + 1);
            if (ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("jpg")) {
                return ext;
            }
        }
        return ext;
    }

    public static boolean isEmpty(Object o) {
        return StringUtils.isEmpty(o);
    }

    //获得web项目根目录
    public static String getWebRootPath(HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        return context.getRealPath("/");
    }

    //获得downloadFilePath
    public static String getDownloadFilePath(HttpServletRequest request) {
        return getWebRootPath(request) + File.separator + "download_files";
    }

    //获得uploadFilePath
    public static String getUploadFilePath(HttpServletRequest request) {
        return getWebRootPath(request) + File.separator + "upload_files";
    }

    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    //获取当前时间
    public static String getCurrentTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }




}
